package com.judian.smart.util;

import android.app.Activity;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.provider.Settings;
import android.view.WindowManager;

import com.judian.smart.activity.MainActivity;
import com.judian.smart.base.MyApplication;

/**
 * author: Peter
 * date: 2019/08/30 星期五 14:50
 * desc: 音量控制管理类   音量亮度工具
 */
public class VolumeLightUtils {
    private static VolumeLightUtils mVolumeLightUtils;

    public static VolumeLightUtils getInstance() {
        if (mVolumeLightUtils == null) {
            mVolumeLightUtils = new VolumeLightUtils();
        }
        return mVolumeLightUtils;
    }

    //----------------------------- 音量 --------------------------------

    // 获取系统设置音量
    public int getVolume() {
        AudioManager audioManager = (AudioManager) MyApplication.getInstance().getSystemService(Context.AUDIO_SERVICE);
        return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    }


    // 设置所有的音量
    public void setAllVolume(int volume) {
        setVolume(volume);
        setSystemVolume(volume);
        setRingVolume(volume);
        showVolumeBar(MyApplication.getInstance());
    }

    // 调高音量
    public void setUpVolume() {
        if (getVolume() == 100) {
            MainActivity.playSound(5, "已是最高音量");
        } else {
            AudioManager audioManager = (AudioManager) MyApplication.getInstance().getSystemService(Service.AUDIO_SERVICE);
            for (int i = 0; i < 2; i++) {
                audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
            }
        }
    }

    // 调低音量
    public void setDownVolume() {
        if (getVolume() == 0) {
            MainActivity.playSound(5, "已是最低音量");
        } else {
            AudioManager audioManager = (AudioManager) MyApplication.getInstance().getSystemService(Service.AUDIO_SERVICE);
            for (int i = 0; i < 2; i++) {
                audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
            }
        }
    }

    /**
     * 设置系统音乐音量
     */
    private void setVolume(int volume) {
        AudioManager audioManager = (AudioManager) MyApplication.getInstance().getSystemService(Context.AUDIO_SERVICE);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND);
    }

    /**
     * 设置系统音量
     */
    private void setSystemVolume(int volume) {
        AudioManager audioManager = (AudioManager) MyApplication.getInstance().getSystemService(Context.AUDIO_SERVICE);
        audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, volume, AudioManager.FLAG_PLAY_SOUND);
    }

    /**
     * 设置系统铃声音量
     */
    private void setRingVolume(int volume) {
        AudioManager audioManager = (AudioManager) MyApplication.getInstance().getSystemService(Context.AUDIO_SERVICE);
        audioManager.setStreamVolume(AudioManager.STREAM_RING, volume, AudioManager.FLAG_PLAY_SOUND);
    }

    //显示音量条
    private void showVolumeBar(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
        audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI);
    }


    //----------------------------- 亮度 --------------------------------


    /**
     * 判断是否开启自动亮度调节
     *
     * @return 当前是否开启了自动亮度
     */
    public boolean isAutoBrightness(Context context) {
        ContentResolver resolver = context.getContentResolver();
        boolean automicBrightness = false;
        try {
            automicBrightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return automicBrightness;
    }

    /**
     * 获取当前屏幕亮度
     *
     * @return 亮度值 0-255
     */
    public int getScreenBrightness() {
        int nowBrightnessValue = 0;
        ContentResolver resolver = MyApplication.getInstance().getContentResolver();
        try {
            nowBrightnessValue = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nowBrightnessValue;
    }

    /**
     * 开启/关闭自动亮度调节
     *
     * @param flag 是否打开亮度调节
     */
    public boolean autoBrightness(Context context, boolean flag) {
        int value;
        if (flag) {
            value = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; //开启
        } else {
            value = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;//关闭
        }
        return Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, value);
    }

    /**
     * 设置亮度,退出app也能保持该亮度值
     *
     * @param brightness 设置的亮度值(0-255)
     */
    public void saveBrightness(int brightness) {
        ContentResolver resolver = MyApplication.getInstance().getContentResolver();
        Uri uri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
        Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
        resolver.notifyChange(uri, null);
    }

    //调高亮度
    public void saveUpBrightness() {
        int currentLight = getScreenBrightness();
        if (currentLight == 255) {
            MainActivity.playSound(5, "已是最高亮度");
        } else {
            int offset = currentLight + 30;
            if (offset - 255 < 0) {
                saveBrightness(offset);
            } else {
                saveBrightness(255);
            }
        }
    }

    //调低亮度
    public void saveDownBrightness() {
        int currentLight = getScreenBrightness();
        if (currentLight == 0) {
            MainActivity.playSound(5, "已是最低亮度");
        } else {
            int offset = currentLight - 30;
            if (255 - offset <= 225) {
                saveBrightness(offset);
            } else {
                saveBrightness(0);
            }
        }
    }

    /**
     * 只设置当前activity显示的亮度
     *
     * @param brightness 设置的亮度值(0-255)
     */
    public void etBrightness(Activity activity, int brightness) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.screenBrightness = (float) brightness * (1f / 255f);
        activity.getWindow().setAttributes(lp);
    }


}
